﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using NUnit.Framework;
using SharedData;

namespace TestSharedData
{
    [TestFixture]
    public class TestDataFromDataGrid
    {
        private DataFromDataGrid dataForTransfer;
        private int counter = 0;
        private int x = 10;//размер таблицы с тестовыми данными
        private int y = 15;

        [SetUp]
        public void init()
        {
            String pathToFile = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) 
                + "/ClusterizationTest" + counter++ + ".xml";
            XmlWriter writer = new XmlTextWriter(pathToFile, System.Text.Encoding.UTF8);
            XmlSerializer serialzer = new XmlSerializer(typeof(DataFromDataGrid));

            dataForTransfer = new DataFromDataGrid();
            dataForTransfer.Characteristics = new List<List<double>>();
            dataForTransfer.Names = new string[x];
            dataForTransfer.Names[0] = "a";
            dataForTransfer.Names[1] = "second characteristic";
            dataForTransfer.Names[2] = "r";
            dataForTransfer.Names[3] = "e";
            dataForTransfer.Names[4] = "f";
            dataForTransfer.Names[5] = "b";
            dataForTransfer.Names[6] = "c";
            dataForTransfer.Names[7] = "gh";
            dataForTransfer.Names[8] = "sdf";
            dataForTransfer.Names[9] = "d";

            dataForTransfer.CharacteristicsNames = new string[y];
            dataForTransfer.CharacteristicsNames[0] = "z";
            dataForTransfer.CharacteristicsNames[1] = "y";
            dataForTransfer.CharacteristicsNames[2] = "x";
            dataForTransfer.CharacteristicsNames[3] = "w";
            dataForTransfer.CharacteristicsNames[4] = "u";
            dataForTransfer.CharacteristicsNames[5] = "v";
            dataForTransfer.CharacteristicsNames[6] = "t";
            dataForTransfer.CharacteristicsNames[7] = "r";
            dataForTransfer.CharacteristicsNames[8] = "h";
            dataForTransfer.CharacteristicsNames[9] = "l";
            dataForTransfer.CharacteristicsNames[10] = "n";
            dataForTransfer.CharacteristicsNames[11] = "q";
            dataForTransfer.CharacteristicsNames[12] = "m";
            dataForTransfer.CharacteristicsNames[13] = "j";
            dataForTransfer.CharacteristicsNames[14] = "g";

            dataForTransfer.Id = new int[x];

            Random rnd = new Random();
            for (int i = 0; i < x; i++)
            {
                dataForTransfer.Id[i] = i;
                dataForTransfer.Characteristics.Add(new List<double>());
                for (int j = 0; j < y; j++)
                {
                    dataForTransfer.Characteristics[i].Add(rnd.NextDouble());
                }
            }

            serialzer.Serialize(writer, dataForTransfer);
            writer.Close();
        }

        [Test]
        public void TestSerialization()
        {
            Assert.IsTrue(true);
        }

        [Test]
        public void Deserialization()
        {
            String pathToFile = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "/ClusterizationTest"
                + (counter - 1).ToString() + ".xml";
            XmlReader reader = new XmlTextReader(pathToFile);
            XmlSerializer deserialzer = new XmlSerializer(typeof(DataFromDataGrid));
            Assert.IsTrue(deserialzer.CanDeserialize(reader));
            DataFromDataGrid receivedData = (DataFromDataGrid)deserialzer.Deserialize(reader);
            for (int i = 0; i < x; i++)
            {
                Assert.AreEqual(receivedData.Names[i], dataForTransfer.Names[i]);
                Assert.AreEqual(receivedData.Id[i], dataForTransfer.Id[i]);
                for (int j = 0; j < y; j++)
                {
                    Assert.AreEqual(receivedData.Characteristics[i][j], dataForTransfer.Characteristics[i][j]);
                }
            }
            for (int j = 0; j < y; j++)
            {
                Assert.AreEqual(receivedData.CharacteristicsNames[j], dataForTransfer.CharacteristicsNames[j]);
            }
        }
    }
}
